package com.goldplusgold.tradedelay.app.market.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.fragment.BaseFragment;
import com.goldplusgold.support.lib.utils.StatusBarUtil;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.constant.IntentConstant;
import com.goldplusgold.tradedelay.app.market.activity.QuotationChartActivity;
import com.goldplusgold.tradedelay.app.market.eventbus.DynaMessageEvent;
import com.goldplusgold.network.lib.http.protobuf.Market;
import com.goldplusgold.support.lib.utils.StringConvertNumber;
import com.goldplusgold.support.lib.widget.TitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;


public class MarketFragment extends BaseFragment {


    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.txt_au_price)
    TextView txtAuPrice;
    @BindView(R.id.txt_au_rose_rate)
    TextView txtAuRoseRate;
    @BindView(R.id.txt_au_rose_money)
    TextView txtAuRoseMoney;
    @BindView(R.id.txt_ag_price)
    TextView txtAgPrice;
    @BindView(R.id.txt_ag_rose_rate)
    TextView txtAgRoseRate;
    @BindView(R.id.txt_ag_rose_money)
    TextView txtAgRoseMoney;
    @BindView(R.id.txt_mau_price)
    TextView txtMauPrice;
    @BindView(R.id.txt_mau_rose_rate)
    TextView txtMauRoseRate;
    @BindView(R.id.txt_mau_rose_money)
    TextView txtMauRoseMoney;
    @BindView(R.id.id_gold_trade_layout)
    LinearLayout mGoldTradeLayout;
    @BindView(R.id.id_silver_trade_layout)
    LinearLayout mSilverTradeLayout;
    @BindView(R.id.id_mini_gold_trade_layout)
    LinearLayout mMiniGoldTradeLayout;

    public static MarketFragment newInstance() {
        MarketFragment fragment = new MarketFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_market;
    }


    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setColor(getActivity(), getResources().getColor(com.goldplusgold.support.lib.R.color.colorPrimary));
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText(getString(R.string.quotation));
    }

    @OnClick({R.id.id_gold_trade_layout, R.id.id_silver_trade_layout, R.id.id_mini_gold_trade_layout})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_gold_trade_layout:
                actionQuotationChartPage("Au(T+D)", "黄金延期");
                break;
            case R.id.id_silver_trade_layout:
                actionQuotationChartPage("Ag(T+D)", "白银延期");
                break;
            case R.id.id_mini_gold_trade_layout:
                actionQuotationChartPage("mAu(T+D)", "迷你黄金延期");
                break;
        }
    }

    public void actionQuotationChartPage(String tradeCode, String tradeType) {
        Intent intent = new Intent(getActivity(), QuotationChartActivity.class);
        intent.putExtra(IntentConstant._TRADE_CODE, tradeCode);
        intent.putExtra(IntentConstant._TRADE_TYPE, tradeType);
        startActivity(intent);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onQuoteDynaEventBus(DynaMessageEvent event) {
        if (event.getType() == Market.Category.QUOTEDYNA) {
            if (isAvailableActivity()) {
                fillMarketData(event.getIdentification(), event.getQuoteDyna());
            }
        }
    }

    //填充行情数据
    private void fillMarketData(Market.Identification identification, Market.QuoteDyna quoteDyna) {
        if (identification.getInstType() == Market.InstType.AU_TD) {
            setMarketItem(txtAuPrice, txtAuRoseRate, txtAuRoseMoney, quoteDyna);
        } else if (identification.getInstType() == Market.InstType.AG_TD) {
            setMarketItem(txtAgPrice, txtAgRoseRate, txtAgRoseMoney, quoteDyna);
        } else if (identification.getInstType() == Market.InstType.MAU_TD) {
            setMarketItem(txtMauPrice, txtMauRoseRate, txtMauRoseMoney, quoteDyna);
        }
    }

    private void setMarketItem(TextView price, TextView riseRate, TextView riseMoney, Market.QuoteDyna quoteDyna) {
        price.setText(quoteDyna.getLastPrice());
        riseRate.setText(quoteDyna.getUpDownRate());
        riseMoney.setText(quoteDyna.getUpDown());
        setTextColor(price, riseRate, riseMoney, quoteDyna.getLastPrice(), quoteDyna.getLastSettle(), quoteDyna.getUpDownRate());
    }

    private void setTextColor(TextView price, TextView roseRate, TextView roseMoney, String lastPrice, String lastSettle, String upDownRate) {
        double lastSettleDouble = StringConvertNumber.parseDouble(lastSettle);
        double lastPriceDouble = StringConvertNumber.parseDouble(lastPrice);

        if (lastSettleDouble > lastPriceDouble) {
            price.setTextColor(getResources().getColor(R.color.color_price_down));
        } else if (lastSettleDouble == lastPriceDouble) {
            price.setTextColor(getResources().getColor(R.color.td_color_gray_two));
        } else {
            price.setTextColor(getResources().getColor(R.color.shallow_red));
        }

        String flag = upDownRate.substring(0, 1);
        if ("+".equals(flag)) {
            roseRate.setBackgroundResource(R.drawable.market_red_btn_shape);
            roseMoney.setBackgroundResource(R.drawable.market_red_btn_shape);
        } else if ("-".equals(flag)) {
            roseRate.setBackgroundResource(R.drawable.market_green_btn_shape);
            roseMoney.setBackgroundResource(R.drawable.market_green_btn_shape);
        } else {
            roseRate.setBackgroundResource(R.drawable.market_gray_btn_shape);
            roseMoney.setBackgroundResource(R.drawable.market_gray_btn_shape);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}